package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.Organization;
import com.xmy.cultivate.entity.Quarter;
import com.xmy.cultivate.entity.Staff;
import com.xmy.cultivate.mapper.OrganizationMapper;
import com.xmy.cultivate.service.IOrganizationService;
import com.xmy.cultivate.service.IQuarterService;
import com.xmy.cultivate.service.IStaffService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 组织机构 前端控制器
 * </p>
 *
 * @author min
 * @since 2022-07-19
 */
@RestController
@RequestMapping("/api/schoolQuarter")
@Scope("prototype")
public class SchoolQuarterController extends BaseController {

    @Autowired
    @Lazy
    IOrganizationService iOrganizationService;

    @Autowired
    @Lazy
    IQuarterService iQuarterService;

    /**
     * 获取校区
     */
    @GetMapping()
    public Result findAll(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                          @RequestParam(value = "pageSize", defaultValue = "1000") Integer pageSize,
                          @RequestParam(value = "schoolId", defaultValue = "") String schoolId,
                          @RequestParam(value = "pid", defaultValue = "", required = false) String pid,// 0为大校区
                          @RequestParam(value = "yearPart", defaultValue = "", required = false) String yearPart,
                          @RequestParam(value = "quarterNum", defaultValue = "", required = false) String quarterNum) {
        try {
            String yearTemp = yearPart;
            Quarter quarterNow = iQuarterService.getNowQuarter();
            if (StringUtils.isBlank(yearPart)) {
                yearPart = quarterNow.getYearPart().toString();
            }
            if (StringUtils.isBlank(quarterNum)) {
                quarterNum = quarterNow.getNum().toString();
                /*if (quarterNum.equals("1")){
                    if (StringUtils.isBlank(yearTemp)){
                        Integer yearPart2 = Integer.parseInt(yearPart) + 1;
                        yearPart = yearPart2.toString();
                    }
                }*/
            }else {
                if (quarterNum.equals("1") && !quarterNow.getNum().toString().equals(quarterNum)){
                    if (StringUtils.isBlank(yearTemp)){
                        Integer yearPart2 = Integer.parseInt(yearPart) + 1;
                        yearPart = yearPart2.toString();
                    }
                }
            }

            IPage<Organization> iPage = new Page<>(pageNum, pageSize);
            QueryWrapper<Organization> organQueryWrapper = new QueryWrapper<>();
            organQueryWrapper.eq("deleted", 0);
            String sql = "";
            if (StringUtils.isNotBlank(pid)) {
                sql = "select school_id from school_quarter where year_part = " + yearPart + " and quarter_num = " + quarterNum + " and deleted = 0 and pid = " + pid;
            } else {
                sql = "select school_id from school_quarter where year_part = " + yearPart + " and quarter_num = " + quarterNum + " and deleted = 0";
            }
            organQueryWrapper.inSql("id", sql);
            if (StringUtils.isNotBlank(schoolId)) {
                organQueryWrapper.eq("id", schoolId);
            } else {
                if (this.isSuper.equals(1)) {
                } else {
                    organQueryWrapper.in("id", this.baseSchoolIdList);
                }
            }
            iOrganizationService.page(iPage, organQueryWrapper);
            return new Result(Code.OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()));
        } catch (Exception e) {
            return new Result(Code.ERROR, "", e.getMessage());
        }
    }
}
